package com.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository("studentDao")
public class StudentDaoImpl implements StudentDao {
    //定义JdbcTemplate对象
    private JdbcTemplate jdbcTemplate;
    @Autowired

//    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
//        this.jdbcTemplate = jdbcTemplate;
//    }

    @Override
    public int addStudent(Student student) {
        //添加学生
        String sql = "insert into spring.student(id,username,password,course) values(?,?,?,?)";
        int result = jdbcTemplate.update(sql, student.getId(), student.getUsername(), student.getPassword(), student.getCourse());
        return result;
    }

    @Override
    public int updateStudentById(Student student) {
        //修改学生信息
        String sql = "update spring.student set username = ?,password = ?,course = ? where id = ?";
        int result = jdbcTemplate.update(sql, student.getUsername(), student.getPassword(), student.getCourse(), student.getId());
        return result;
    }

    @Override
    public int updatePasswordByIdt(int id, int password) {
            //修改密码
        String sql = "update spring.student set password = ? where id = ?";
        int result = jdbcTemplate.update(sql, password, id);
        return result;
    }

    @Override
    public int deleteStudentById(int id) {
        //删除学生
        String sql = "delete from spring.student where id = ?";
        int result = jdbcTemplate.update(sql, id);
        return result;
    }
}
